package org.amse.korovkin.ciphering.view;

import java.awt.event.ActionEvent;
import javax.swing.*;
import org.amse.korovkin.ciphering.Main;
import org.amse.korovkin.ciphering.model.Model;
import org.amse.korovkin.ciphering.model.cipher.IKey;

public class KeyJDialog extends MyJDialog {
    private static final String GENERATE_ICON_PATH = "org/amse/korovkin/ciphering" +
            "/icons/key-16.png";

    public KeyJDialog(CipheringJFrame jFrame) {
        super(jFrame, "Key");
        jl.setText("Key");
        action.setAction(new GenerateAction(this));
        setVisible(true);
    }

    @Override
    public void pressOk() {
        if (tf.getText().length() != 0) {
            parent.setKeyPath(tf.getText());
            returnValue = 1;
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(this, "Please choose KeyFile", "Error"
                    , JOptionPane.ERROR_MESSAGE);
        }
    }

    public class GenerateAction extends AbstractAction {
        MyJDialog parent;

        public GenerateAction(MyJDialog jFrame) {
            parent = jFrame;
            putValue(SHORT_DESCRIPTION, "Generate Random KeyFile");
            ClassLoader cl = Main.class.getClassLoader();
            putValue(LARGE_ICON_KEY, new ImageIcon(cl.getResource(GENERATE_ICON_PATH)));
        }
        public void actionPerformed(ActionEvent e) {
            Model.getInstance().generateRandomKey();
            parent.setFilePath(IKey.KEY_PATH);
        }
    }
}
